﻿using AutoMapper;
using MediatR;
using MyMES.Domain.W.Liststructure;
using MyMES.Domain.zmc;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Read.API.Application.Commends.Productionorderss;
using MyMES.Read.API.Dtos;

namespace MyMES.Read.API.Application.CommendHandlers.Productionorderss
{
    public class GetMaterialmanagementProductionCommendHandlers : IRequestHandler<GetMaterialmanagementProductionCommends, APIResult<List<MaterialmanagementProductionDtos>>>
    {
        private readonly IBaseRepository<Productionorders> baseRepository;
        private readonly IBaseRepository<Materialmanagement>  materiaRepository;
        private readonly IMapper mapper;

        public GetMaterialmanagementProductionCommendHandlers(IBaseRepository<Productionorders> baseRepository, IBaseRepository<Materialmanagement> materiaRepository, IMapper mapper)
        {
            this.baseRepository = baseRepository;
            this.materiaRepository = materiaRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 获取生产物品数据---生产详情
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<List<MaterialmanagementProductionDtos>>> Handle(GetMaterialmanagementProductionCommends request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<List<MaterialmanagementProductionDtos>> result = new APIResult<List<MaterialmanagementProductionDtos>>();
                result.Code = APIEnum.查询成功;
                result.Msg = "查询成功";
                string sql = $"select a.*,b.Materialnumber,b.Materialname,b.Specification,b.Unit,b.Materialproduct,b.sort FROM productionorders a LEFT JOIN Materialmanagement b on a.MId=b.MId where ProductionordersId='{request.ProductionordersId}'";

                var produc = baseRepository.SelectAsync(sql).Result;
                var mater = materiaRepository.SelectAsync(sql).Result;

                var producs = mapper.Map<List<MaterialmanagementProductionDtos>>(produc);
                var materis = mapper.Map<List<MaterialmanagementProductionDtos>>(mater);
                foreach (var item in producs)
                {
                    item.Materialnumber = materis.Where(x => x.MId == item.MId).FirstOrDefault().Materialnumber;
                    item.Materialname = materis.Where(x => x.MId == item.MId).FirstOrDefault().Materialname;
                    item.Specification = materis.Where(x => x.MId == item.MId).FirstOrDefault().Specification;
                    item.Unit = materis.Where(x => x.MId == item.MId).FirstOrDefault().Unit;
                    item.Materialproduct = materis.Where(x => x.MId == item.MId).FirstOrDefault().Materialproduct;
                    item.sort = materis.Where(x => x.MId == item.MId).FirstOrDefault().sort;

                }
                result.data = producs;
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
